import './index.scss'
export default ngModule => {
  ngModule.component('multipleLevel', {
    template: require('./index.html'),
    bindings: {
      data: '<',
      level: '<',
      aggFields: '<'
    },
    controllerAs: '$ctrl',
    controller($scope, $compile, $timeout) {
      const $ctrl = this;

      Object.assign($ctrl, {
        list: [],
        showList: [],
        showFlags: [],
        showNum: 5,
        tableData:[],
        toggleShow(index, flag) {
          $ctrl.showFlags.forEach((element, index) => {
            $ctrl.showFlags[index] = false;
          });
          $ctrl.showFlags[index] = flag
        },
        showMore() {
          $ctrl.showNum += 5;
          $ctrl.showList = $ctrl.list.slice(0,$ctrl.showNum);
        },
        $onChanges(e) {
          $ctrl.list = $ctrl.data[$ctrl.aggFields?. [$ctrl.level]]?.buckets??[];
          $ctrl.showList = $ctrl.list.slice(0, $ctrl.showNum);
          $ctrl.showFlags = new Array($ctrl.list.length).fill(false);
          $ctrl.tableData = $ctrl.data.top_hits?.hits?.hits??[];
          console.log($ctrl.tableData);
        }
      })
      console.log($scope, this)
    }
  })
}